package com.by.controller;

import cn.hutool.core.bean.BeanUtil;
import com.by.bean.ResultBean;
import com.by.bean.SupplierContact;
import com.by.group.V;
import com.by.mapper.SupplierContactMapper;
import com.by.query.SupplierContactQuery;
import com.by.service.SupplierContactService;
import com.by.service.SupplierService;
import com.by.utils.Assert;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author :LYJ
 * @version : 1.0
 * @date : 2025/2/18 16:17
 * @desc :
 * @since : JDK17
 */
@RestController
@RequestMapping("/api")
@Api(tags = "供应商联系人")
public class SupplierContactController extends BaseController {
    @Autowired
    SupplierService supplierService;
    @Autowired
    SupplierContactMapper supplierContactMapper;
    @Autowired
    SupplierContactService supplierContactService;

    @PostMapping("/supplierContact/save")
    @ApiOperation("供应商联系人保存")
    public ResultBean save(@Validated @RequestBody SupplierContact supplierContact, BindingResult result) {
        V.validate(result);
        int res = supplierContactMapper.save(supplierContact);
        Assert.isZero(res, "保存失败");
        return success("success", "保存成功");
    }

    @DeleteMapping("/supplierContact/delete/{id}")
    @ApiOperation("供应商联系人删除")
    public ResultBean delete(@PathVariable int id) {
        int res = supplierContactMapper.delete(id);
        Assert.isZero(res, "删除失败");
        return success("success", "删除成功");
    }

    @PutMapping("/supplierContact")
    @ApiOperation("更新供应商联系人信息")
    public ResultBean update(@Validated @RequestBody SupplierContact supplierContact, BindingResult result) {
        V.validate(result);
        Assert.isNull(supplierContact.getId(), "id不存在");
        int res = supplierContactMapper.update(supplierContact);
        Assert.isZero(res, "更新失败");
        return success("success", "更新成功");
    }

    @PostMapping("/supplierContact/list")
    @ApiOperation("根据联系人名称、电话查询")
    public ResultBean queryByName(@RequestBody SupplierContactQuery supplierContactQuery) {
        return success("success", supplierContactMapper.queryByMap(BeanUtil.beanToMap(supplierContactQuery)));
    }


    @PutMapping("/supplierContact/resetPassword")
    @ApiOperation("重置密码(123456)")
    public ResultBean resetPassword(@RequestBody Map<String, Integer> params) {
        //获取id参数
        int id = params.get("id");
        return supplierContactService.resetPassword(id);
    }
}
